﻿
layui.use('table', function () {
    var table = layui.table
        , form = layui.form;  //启用
    table.render({
        elem: '#test'
        , url: '/Touristroute/GetTouristrouteList'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        , cols: [[
            { type: 'checkbox' }
            , { field: 'Id', width: 60, title: 'ID', sort: true, align: 'center' }
            , { field: 'ProductNumber', width: 140, title: '产品编号', align: 'center' }
            , { field: 'OutPlace', width: 110, title: '出发地点', sort: true, align: 'center' }
            , { field: 'OverPlace', width: 100, title: '结束地点', align: 'center' }
            , { field: 'IsThemeTour', width: 90, align: 'center', title: '是否主题', templet: '#switchTpl', unresize: true }
            , { field: 'Price', width: 90, align: 'center', title: '价格' }
            , { field: 'TName', title: '路线名称', align: 'center', width: '30%', minWidth: 70 } //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
            , { field: 'HotelName', width: 100, align: 'center', title: '酒店名称' }
            , { field: 'ScenicName', width: 100, align: 'center', title: '景点名称' }
            , { field: 'TDate', width: 160, align: 'center', templet: "<div>{{ Format(d.TDate, 'yyyy-MM-dd hh:mm:ss')}}</div>", title: '上架时间', sort: true }
            , { field: 'OutDate', width: 160, align: 'center', templet: "<div>{{ Format(d.OutDate, 'yyyy-MM-dd hh:mm:ss')}}</div>", title: '出团日期', sort: true }
            , { fixed: 'right', title: '操作', width: 195, align: 'center', toolbar: '#barDemo' }
        ]]
        , page: true
        , id: 'testReload'//删除
    });
    //监听编辑页启用开关
    form.on('switch(switchEnable)', function (data) {
        $(data.elem).attr('type', 'hidden').val(this.checked ? 1 : 0);
    });

    //批量删除
    $("#DelListByID").click(function () {
        //获取表格复选框的数据集合 checkStatus('testReload');
        var checkStatus = table.checkStatus('testReload');
        var data = checkStatus.data;
        var ListId = "";
        if (data.length == 0) {
            layer.msg("请选中一行",
                {
                    icon: 0,
                    time: 2000
                });
            return;
        }
        for (var i in data) {
            if (i == data.length - 1) {
                ListId += data[i].Id;
            }
            else {
                ListId += data[i].Id + ",";
            }
        };
        layer.confirm('确认要删除吗？', function () {
            $.ajax({
                url: "/Touristroute/BatchDeleteById?ListId=" + ListId,
                type: "post",
                success: function (result) {
                    if (result.Success) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）使用这个弹框那么弹框后续操作就要写在function中
                        }, function () {
                            location.reload();
                        });
                    }
                    else {
                        layer.msg(result.msg, { icon: 0, time: 1000 });
                    }
                }
            })
        });
    });
    //是否主题按钮
    form.on('switch(sexDemo)', function (obj) {
        layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        var tip = "开启";
        var id = this.id;
        var IsThemeTour = this.value;
        if (this.value === "1") {
            tip = "关闭";
            IsThemeTour = 0;
        }
        else {
            IsThemeTour = 1;
        }
        //启用的功能代码
        layer.confirm('真的要' + tip + '吗', function (index) {
            $.ajax({
                url: "/TouristRoute/UpdateTouristRoute?Id=" + id + "&IsThemeTour=" + IsThemeTour,
                type: "Post",
                success: function (result) {
                    if (result.Success) {
                        layer.msg(tip + result.msg, {
                            title: '提示框',
                            icon: 1,
                            time: 1000
                        }, function () {
                            location.reload();//刷新页面
                            layer.close(index);
                        });
                    }
                    else {
                        layer.msg(tip + result.msg, {
                            title: '提示框',
                            icon: 1,
                            time: 1000
                        });
                    }
                }
            });
        });
    });

    //监听工具
    table.on('tool(demo)', function (obj) {
        var data = obj.data;
        //删除
        if (obj.event === 'del') {
            layer.confirm('确认删除行？', function (index) {
                $.ajax({
                    url: "/TouristRoute/DelTouristRoute?Id="+data.Id,
                    type: "post",
                    success: function (result) {
                        if (result.Success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）使用这个弹框那么弹框后续操作就要写在function中
                            }, function () {
                                    location.reload();
                            });
                        }
                        else {
                            layer.msg(result.msg);
                        }
                    }
                })
            });
            //编辑
        } else if (obj.event === 'edit') {
            
            $("#Id").val(data.Id);
            $("#ProductNumber").val(data.ProductNumber);
            $("#OverPlace option[value='" + data.OverPlace + "']").attr("selected", "selected");
            $("#OutPlace option[value='" + data.OutPlace + "']").attr("selected", "selected");
            $("#TName").val(data.TName);
            $("#Price").val(data.Price);
            $("#OutDate").val(Format(data.OutDate, 'yyyy-MM-dd'));
            $("#Duration option[value='" + data.Duration + "']").attr("selected", "selected");
            $("#RouteIntroduce").val(data.RouteIntroduce);
            $("#TDate").val(Format(data.TDate, 'yyyy-MM-dd'));
            //给隐藏的按钮赋值
            if (data.IsThemeTour === 1) {
                $("#isvip em").text("是");
                $("#isvip .layui-form-switch").addClass("layui-form-onswitch");
            } else {
                $("#isvip em").text("否");
                $("#isvip .layui-form-switch").removeClass("layui-form-onswitch");
            }
            $("#HotelId option[value='" + data.HotelId + "']").attr("selected", "selected");
            $("#ScenicSpotId option[value='" + data.ScenicSpotId + "']").attr("selected", "selected");

            //打开编辑路线按钮
            layer.open({
                type: 1,
                title: '添加管理员',
                area: ['700px', ''],
                shadeClose: false,
                content: $('#Rigth'),
            });
            //点击编辑将标题修改成编辑
            $("#rigthName").text("编辑路线");
        }
    });
});

$(function () {
    //打开添加路线按钮
    $("#AddTouristroute").on('click', function () {
        layer.open({
            type: 1,
            title: '添加路线',
            area: ['700px', ''],
            shadeClose: false,
            content: $('#Rigth'),
        });
        //点击添加清空编辑时残留的值
        $("#divContext input").val("");
        $("#divContext select").val("");
        $("#RouteIntroduce").val("");
        //点击添加将标题修改成添加
        $("#rigthName").text("添加路线");
    });

    //添加或编辑旅游路线
    $("#commit").on('click', function () {
        var TouristRoute = {};
        TouristRoute.Id = $("#Id").val();
        TouristRoute.ProductNumber = $("#ProductNumber").val();
        TouristRoute.OutPlace = $("#OutPlace").val();
        TouristRoute.OverPlace = $("#OverPlace").val();
        TouristRoute.TName = $("#TName").val();
        TouristRoute.Price = $("#Price").val();
        TouristRoute.OutDate = $("#OutDate").val();
        TouristRoute.Duration = $("#Duration").val();
        TouristRoute.RouteIntroduce = $("#RouteIntroduce").val();
        TouristRoute.TDate = $("#TDate").val();
        TouristRoute.IsThemeTour =0;
        if ($("#IsThemeTour").is(":Checked")) { TouristRoute.IsThemeTour = 1; }
        TouristRoute.HotelId = $("#HotelId").val();
        TouristRoute.ScenicSpotId = $("#ScenicSpotId").val();
        //根据ID是否为空判断添加还是编辑
        $.ajax({
            url: "/TouristRoute/AddTouristRoute",
            type: "post",
            data: TouristRoute,
            success: function (result) {
                if (result.Success) {                    
                    layer.msg(result.msg, {
                        icon: 1,
                        time: 1000 //2秒关闭（如果不配置，默认是3秒）使用这个弹框那么弹框后续操作就要写在function中
                    }, function () {
                        location.reload(); window.location.href = "/Home/TouristRoute";
                    });
                }
                else {
                    layer.msg(result.msg);
                }
            }
        });
    });

    //查询按钮
    $("#TableReload").on('click', function () {
        var table = layui.table;
        var data = $("#WhereTable").val();
        table.reload('testReload', {
            where: { //设定异步数据接口的额外参数，任意设
                data: data,
                //…
            }
        });
    });
});